package org.vaadin.passwordfield.client.ui;

/**
 * This makes the same colors and hash-algorithm available both on the server
 * and client. Calling {@link #hashColors(String)} with a specific string will
 * always generate the same 'flag'.
 * 
 */
public class PasswordFieldUtils {
    public static final String[] colors = new String[] { "#00B4F0", "#EE7C08",
            "#2EB33C", "#EC0B6D" };

    /**
     * Generates an array of CSS color strings for the given input. The same set
     * of colors are always generated for the same given string.
     * 
     * @param input
     * @return
     */
    public static String[] hashColors(String input) {
        int len = input.length();
        int h = input.hashCode();
        int a = input.substring(0, len / 3).hashCode();
        int b = input.substring(len / 3, (len / 3) * 2).hashCode();
        int c = input.substring((len / 3) * 2).hashCode();
        a = Math.abs(a + h) % colors.length;
        b = Math.abs(b + h / 4) % colors.length;
        c = Math.abs(c + h / 7) % colors.length;
        return new String[] { colors[a], colors[b], colors[c] };
    }
}
